<template>
  <div>
    <el-row>
      <el-col :span="12">
        <el-button type="success" @click="isdraw('新增')">新增</el-button>
        <el-button type="warning" @click="isdraw('修改')">修改</el-button>
        <el-button type="danger" @click="delonData">删除</el-button>
      </el-col>
      <el-col :span="12">
        <el-input placeholder="请输入内容" v-model="input" class="input-with-select">
          <el-select v-model="select" slot="prepend" style="width:130px" placeholder="请选择">
            <el-option label="餐厅名" value="1"></el-option>
            <el-option label="订单号" value="2"></el-option>
            <el-option label="用户电话" value="3"></el-option>
          </el-select>
          <el-button slot="append" icon="el-icon-search"></el-button>
        </el-input>
      </el-col>
    </el-row>
    <div style="margin-top: 30px">
      <!--  -->
      <el-table
        :data="tableData"
        style="width: 100%"
        max-height="700"
        @selection-change="handleSelectionChange"
      >
        <el-table-column fixed type="selection" width="55"></el-table-column>
        <el-table-column type="index" width="50" label="#"></el-table-column>
        <el-table-column prop="title" label="姓名" width="500"></el-table-column>
        <el-table-column prop="author" label="作者" width="100"></el-table-column>
        <el-table-column prop="date" label="时间" width="200"></el-table-column>
        <el-table-column prop="cid" label="类别" width="200"></el-table-column>
        <el-table-column prop="time" label="用时" width="250"></el-table-column>
        <el-table-column fixed="right" label="操作" width="300">
          <template slot-scope="scope">
            <el-button @click.native.prevent="deleteRow(scope.row,'删除')" type="text" size="small">移除</el-button>
            <el-button @click.native.prevent="deleteRow(scope.row,'修改')" type="text" size="small">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--  -->
    </div>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 13, 15, 17]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <!--  -->
    <el-drawer :title="title" :visible.sync="drawer" :before-close="handleClose">
      <div class="demo-drawer__content">
        <el-form :model="form">
          <el-form-item label="题目" :label-width="formLabelWidth">
            <el-input v-model="form.title" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="作者" :label-width="formLabelWidth">
            <el-input v-model="form.author" autocomplete="off" disabled></el-input>
          </el-form-item>
          <el-form-item label="分类" :label-width="formLabelWidth">
            <el-select v-model="form.cid" placeholder="请选择类别">
              <el-option
                v-for="(item,index) in classify"
                :key="index"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="用时" :label-width="formLabelWidth">
            <el-select v-model="form.time" autocomplete="off" placeholder="请选择用时">
              <el-option label="1分钟" value="1"></el-option>
              <el-option label="2分钟" value="2"></el-option>
              <el-option label="3分钟" value="3"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="内容" :label-width="formLabelWidth">
            <el-input v-model="form.content" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div class="demo-drawer__footer" style="margin-left:400px">
          <el-button @click="cancelForm('取消')">取 消</el-button>
          <el-button type="primary" @click="cancelForm('确定')">确 定</el-button>
        </div>
      </div>
    </el-drawer>
    <!--  -->
  </div>
</template>

<script>
import { mapState, mapActions } from "vuex";
export default {
  layout: "mine",
  async fetch({ $axios, store }) {
    await store.dispatch("classify/getData", { pageSize: 70 });
    store.dispatch("msbd/getData", {
      pageSize: 10,
      page: 0
    });
  },
  data() {
    return {
      input: "",
      select: "",
      drawer: false,
      title: "新增",
      currentPage: 1,
      form: {
        author: "situ"
      },
      formLabelWidth: "80px",
      pageSize: 10,
      multipleSelection: []
    };
  },
  methods: {
    ...mapActions("msbd", ["getData", "addData", "editData", "delData"]),
    getTableData() {
      console.log(this.pageSize, this.currentPage);
      this.getData({
        pageSize: this.pageSize,
        page: this.currentPage - 1
      });
    },
    isdraw(v, row) {
      // 如果是复选框的修改
      if (!row) {
        // 点击的是修改 但是没有点击复选框
        if (v == "修改" && this.multipleSelection.length != 1) {
          return;
        }
      }
      if (v == "修改") {
        if (!row) {
          let val = this.multipleSelection[0];
          this.form = { ...val };
        } else {
          // 表格内修改  有row
          this.form = { ...row };
        }
      }
      this.title = v;
      this.drawer = !this.drawer;
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    async delonData() {
      if (!this.multipleSelection) return;
      let ids = this.multipleSelection.map(item => {
        return item.id;
      });
      console.log(ids);
      await this.delData(ids.join(","));
      this.getTableData();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getTableData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getTableData();
    },
    async deleteRow(row, val) {
      console.log(row, val);
      if (val == "删除") {
        await this.delData(row.id);
        this.getTableData();
        console.log(1);
      } else {
        await this.isdraw();
        this.form = { ...row };
      }
    },
    async cancelForm(val) {
      if (val == "确定") {
        if (this.title == "新增") {
          await this.addData(this.form);
        } else {
          await this.editData(this.form);
        }
        this.getTableData();
        this.form = {
          author: "situ"
        };
        this.isdraw();
      } else {
        this.form = {
          author: "situ"
        };
        this.isdraw();
      }
    },
    handleClose(done) {
      done();
    }
  },
  computed: {
    ...mapState("msbd", ["data", "totals"]),
    tableData() {
      let classtableData = JSON.parse(JSON.stringify(this.data));
      classtableData.map(item => {
        this.classify.forEach(element => {
          item.cid == element.id ? (item.cid = element.name) : "";
        });
      });
      return classtableData;
    },
    total() {
      console.log(this.totals);
      return this.totals;
    },
    classify() {
      return this.$store.state.classify.data;
    }
  }
};
</script>

<style scoped>
</style>